home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 242 / Issue 242 - April 2008 - DPCS0408DVD.ISO / Open Source / AutoHotKey / Source / AutoHotkey104705_source.exe / source / clipboard.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-01-09  |  3.4 KB  |  87 lines

  1. /*
  2. AutoHotkey
  3.  
  4. Copyright 2003-2007 Chris Mallett (support@autohotkey.com)
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15. */
  16.  
  17. #ifndef clipboard_h
  18. #define clipboard_h
  19.  
  20. #include "stdafx.h" // pre-compiled headers
  21. #include "defines.h"
  22.  
  23.  
  24. #define CANT_OPEN_CLIPBOARD_READ "Can't open clipboard for reading."
  25. #define CANT_OPEN_CLIPBOARD_WRITE "Can't open clipboard for writing."
  26.  
  27.  
  28. class Clipboard
  29. {
  30. public:
  31.     HGLOBAL mClipMemNow, mClipMemNew;
  32.     char *mClipMemNowLocked, *mClipMemNewLocked;
  33.     size_t mLength;  // Last-known length of the clipboard contents (for internal use only because it's valid only during certain specific times).
  34.     UINT mCapacity;  // Capacity of mClipMemNewLocked.
  35.     BOOL mIsOpen;  // Whether the clipboard is physically open due to action by this class.  BOOL vs. bool improves some benchmarks slightly due to this item being frequently checked.
  36.  
  37.     // It seems best to default to many attempts, because a failure
  38.     // to open the clipboard may result in the early termination
  39.     // of a large script due to the fear that it's generally
  40.     // unsafe to continue in such cases.  Update: Increased default
  41.     // number of attempts from 20 to 40 because Jason (Payam) reported
  42.     // that he was getting an error on rare occasions (but not reproducible).
  43.     ResultType Open();
  44.     HANDLE GetClipboardDataTimeout(UINT uFormat);
  45.  
  46.     // Below: Whether the clipboard is ready to be written to.  Note that the clipboard is not
  47.     // usually physically open even when this is true, unless the caller specifically opened
  48.     // it in that mode also:
  49.     bool IsReadyForWrite() {return mClipMemNewLocked != NULL;}
  50.  
  51.     #define CLIPBOARD_FAILURE UINT_MAX
  52.     size_t Get(char *aBuf = NULL);
  53.  
  54.     ResultType Set(char *aBuf = NULL, UINT aLength = UINT_MAX); //, bool aTrimIt = false);
  55.     char *PrepareForWrite(size_t aAllocSize);
  56.     ResultType Commit(UINT aFormat = CF_TEXT);
  57.     ResultType AbortWrite(char *aErrorMessage = "");
  58.     ResultType Close(char *aErrorMessage = NULL);
  59.     char *Contents()
  60.     {
  61.         if (mClipMemNewLocked)
  62.             // Its set up for being written to, which takes precedence over the fact
  63.             // that it may be open for read also, so return the write-buffer:
  64.             return mClipMemNewLocked;
  65.         if (!IsClipboardFormatAvailable(CF_TEXT))
  66.             // We check for both CF_TEXT and CF_HDROP in case it's possible for
  67.             // the clipboard to contain both formats simultaneously.  In this case,
  68.             // just do this for now, to remind the caller that in these cases, it should
  69.             // call Get(buf), providing the target buf so that we have some memory to
  70.             // transcribe the (potentially huge) list of files into:
  71.             return IsClipboardFormatAvailable(CF_HDROP) ? "<<>>" : "";
  72.         else
  73.             // Some callers may rely upon receiving empty string rather than NULL on failure:
  74.             return (Get() == CLIPBOARD_FAILURE) ? "" : mClipMemNowLocked;
  75.     }
  76.  
  77.     Clipboard() // Constructor
  78.         : mIsOpen(false)  // Assumes our app doesn't already have it open.
  79.         , mClipMemNow(NULL), mClipMemNew(NULL)
  80.         , mClipMemNowLocked(NULL), mClipMemNewLocked(NULL)
  81.         , mLength(0), mCapacity(0)
  82.     {}
  83. };
  84.  
  85.  
  86. #endif
  87.